package com.ee.bruscar.configuration.parser;

import java.io.InputStream;

import com.ee.bruscar.configuration.InterfaceConfiguration;
import com.ee.bruscar.configuration.hint.FieldHint;
import com.ee.bruscar.configuration.hint.ListModelHint;
import com.ee.bruscar.configuration.hint.MethodHint;
import com.ee.bruscar.configuration.hint.ModelHint;
import com.thoughtworks.xstream.XStream;

public class XStreamConfig
{
	private static XStream xstream;
	
	public static Object fromXML( InputStream stream )
	{
		return getXstream().fromXML(stream);
	}
	
	private static XStream getXstream()
	{
		if( xstream == null )
		{
			xstream = new XStream();
			xstream.alias("methodHint", MethodHint.class);
			xstream.useAttributeFor( MethodHint.class, "methodName");
			xstream.useAttributeFor(MethodHint.class, "value");
			xstream.aliasField("name", MethodHint.class, "methodName");
			
			
			xstream.alias("modelHint", ModelHint.class);
			xstream.useAttributeFor( ModelHint.class, "hintClass");
			xstream.useAttributeFor( ModelHint.class, "id");
			xstream.useAttributeFor( ModelHint.class, "ref");
			xstream.addImplicitCollection( ModelHint.class, "fieldHints");
			xstream.useAttributeFor( ModelHint.class, "range");
			
			
			xstream.alias("fieldHint", FieldHint.class);
			xstream.useAttributeFor( FieldHint.class, "name");
			xstream.useAttributeFor( FieldHint.class, "value");
			xstream.useAttributeFor( FieldHint.class, "ref");
			
			xstream.alias("bruscarConfig", InterfaceConfiguration.class);
			xstream.useAttributeFor( InterfaceConfiguration.class, "interfaceToHonour");
			xstream.useAttributeFor( InterfaceConfiguration.class, "stubClass");
			xstream.useAttributeFor( InterfaceConfiguration.class, "srcPath" );
		}
		return xstream;
	}
}
